#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_time_0.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#if defined(TIME_0_SKIP)
#else
void view_time_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = sport_get_present_step();
        disp_ctrl.com_data.i_temp[3] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[5] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.second;
        disp_ctrl.com_data.i_temp[7] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[7])
        {
            disp_ctrl.com_data.i_temp[7] += (disp_ctrl.com_data.i_temp[4] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[4] %= 12;
            if (disp_ctrl.com_data.i_temp[4] == 0)
            {
                disp_ctrl.com_data.i_temp[4] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[8] = UIdatetime.date.day;
        disp_ctrl.com_data.i_temp[9] = get_present_week();
        disp_ctrl.com_data.i_temp[10] = battery_ctrl_if_charging();
        disp_ctrl.auto_refresh_time = 1000;
    }
    int x = 148;
    int y = 98;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_1400_ring_148_98);

    x += 42;
    y += 42;
    disp_draw_img_rotate(gImage_second, 0, 30, (x << 16) | y, disp_ctrl.com_data.i_temp[6] * 6);

    x = 90;
    y = 42;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_1101_week_1_90_42 + disp_ctrl.com_data.i_temp[9] - 1);
    x = 27;
    y = 68;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_1300_time_0 + disp_ctrl.com_data.i_temp[4] / 10);
    x = 61;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_1300_time_0 + disp_ctrl.com_data.i_temp[4] % 10);
    x = 121;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_1300_time_0 + disp_ctrl.com_data.i_temp[5] / 10);
    x = 153;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_1300_time_0 + disp_ctrl.com_data.i_temp[5] % 10);

    // 0 14
    // 0 40
    x = 97;
    y = 82;
    disp_draw_user_image(x, y, bmp_time_0_point);
    y += 40 - 14;
    disp_draw_user_image(x, y, bmp_time_0_point);

    x = 83;
    y = 10;
    // #warning debug
    //     disp_ctrl.com_data.i_temp[0] = 0;
    if (disp_ctrl.com_data.i_temp[0])
    {

        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }

    x = 35;
    y = 145;
    disp_draw_user_image(x, y, bmp_time_0_steps);

    x = 35;
    y = 175;
    disp_draw_user_image(x, y, bmp_time_0_heart_rate);

    x = 133;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    // #warning debug
    //     disp_ctrl.com_data.i_temp[10] = 0;
    //     disp_ctrl.com_data.i_temp[1] = 4;
    if (disp_ctrl.com_data.i_temp[10])
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    { // 2 2 7 2 12 2
        // 4x8 0x9cd3
        uint16_t color = 0x9cd3;
        if (disp_ctrl.com_data.i_temp[1] == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < disp_ctrl.com_data.i_temp[1]; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }
    x = 123;
    y = 38;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[8], 2, 11, num_time_0_9x15_TAB);

    // #warning debug
    //     disp_ctrl.com_data.i_temp[2] = 12345;
    x = 66;
    y = 146;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2], 11, num_time_0_9x15_TAB);
    y = 175;
    // #warning debug
    //     disp_ctrl.com_data.i_temp[3] = 78;
    if (disp_ctrl.com_data.i_temp[3] == 0)
    {
        //--
        disp_draw_rect_color(x, y + 5, x + 9, y + 6, RGB565_COLOR_WHITE);
        disp_draw_rect_color(x + 11, y + 5, x + 11 + 9, y + 6, RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[3], 11, num_time_0_9x15_TAB);
    }
}
#endif
